from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_deepseek import ChatDeepSeek

from .prompt_template import system_template_text, user_template_text
from .xiaohongshu_model import Xiaohongshu


def generate_xiaohongshu(theme, api_key):
    prompt = ChatPromptTemplate.from_messages([
        ("system", system_template_text),
        ("human", user_template_text)
    ])

    model = ChatDeepSeek(model="deepseek-chat", api_key=api_key)
    output_parser = PydanticOutputParser(pydantic_object=Xiaohongshu)

    chain = prompt | model | output_parser
    result = chain.invoke({
        "parser_instructions": output_parser.get_format_instructions(),
        "theme": theme
    })
    return result
